package org.icress.fusion.b2b

import java.util.Date;

import org.icress.fusion.shiro.User;

class SalesOrderDetailDetail{//客户订单子单身

    def salesOrderService
    
    SalesOrderDetail salesOrderDetail//客户订单单身
    String serialNumber//序号
    Product product//产品，品号
    BigDecimal quantity//数量
    BigDecimal price//标准单价
    BigDecimal discount//标准折扣率
    BigDecimal specialDiscount//特价折扣
    BigDecimal finalDiscount//最终折扣
    BigDecimal finalPrice//最终单价
    BigDecimal amount//合计金额
    BigDecimal specialAmount//特价金额
    BigDecimal finalAmount//最终销售金额
    ContractDetail contractDetail//OA特价申请单明细
    
    static belongsTo = [salesOrderDetail : SalesOrderDetail]
    
    static constraints = {
        salesOrderDetail(nullable:false)
        serialNumber(nullable:false, blank:false, unique : 'salesOrderDetail', maxSize : 2)
        product(nullable:false)
        quantity(nullable:false, scale : 3, min : new BigDecimal("0.001")) 
        price(nullable:false, min : new BigDecimal(0), scale:6, validator:{val, obj ->
            return obj.salesOrderService.validatePrice(obj.product.id, val)
        })
        discount(nullable:false, scale:4)
        specialDiscount(nullable:false, scale:4, validator:{val, obj ->
            return obj.salesOrderService.validateSpecialDiscount(obj.product.id, val)
        })
        finalDiscount(nullable:false, scale:4)
        finalPrice(nullable:false, scale:6)
        amount(nullable:false, scale:2)
        specialAmount(nullable:false, scale:2)
        finalAmount(nullable:false, scale:2)
        contractDetail(nullable:true)
        
    }
    
    String toString() {
        return "$serialNumber"
    }
    
    static mapping = {
        table 'B2B_SALESORDERDETAILDETAIL'
        /*
        finalDiscount formula : 'discount * (1 - special_discount)'
        finalPrice formula : 'price * discount * (1 - special_discount)'
        amount formula: 'quantity * price * discount'
        specialAmount formula : 'quantity * price * discount * special_discount'
        finalAmount formula : 'quantity * price * discount * (1 - special_discount)'
        */
    }
}
